//
//  GTVideoCoverViewCollectionViewCell.m
//  SampleApp2
//
//  Created by GXHC on 2023/6/19.
//

#import "GTVideoCoverViewCollectionViewCell.h"
#import <AVFoundation/AVFoundation.h>
#import "../Model/GTVideoPlayer.h"
#import "GTVideoToolBar.h"
@interface GTVideoCoverViewCollectionViewCell()


@property(nonatomic, strong, readwrite) UIImageView *coverView;
@property(nonatomic, strong, readwrite) UIImageView *playButton;
@property(nonatomic, copy, readwrite)NSString *videoUrl;
@property(nonatomic, strong, readwrite)GTVideoToolBar *videoToolBar;

@end
@implementation GTVideoCoverViewCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame{
	self = [super initWithFrame:frame];
	if(self){
		[self addSubview:({
					self.coverView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height-GTVideoToolBarHeight)];
					self.coverView;
		})];
		
		[self addSubview:({
			self.playButton = [[UIImageView alloc] initWithFrame:CGRectMake((frame.size.width - 50)/2, (frame.size.height-GTVideoToolBarHeight - 50)/2, 50, 50)];
			self.playButton.image = [UIImage imageNamed:@"videoPlay"];
			self.playButton;
		})];
		
		UITapGestureRecognizer *tapGusture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(_tapToPlay)];
		[self addGestureRecognizer:tapGusture];
		
		[self addSubview:({
			self.videoToolBar = [[GTVideoToolBar alloc]initWithFrame:CGRectMake(0, self.coverView.bounds.size.height, frame.size.width, GTVideoToolBarHeight)];
			self.videoToolBar;
		})];
		
	}
	return self;
}


#pragma mark - public

- (void)layoutWithVideoCoverUrl:(NSString *)videoCoverUrl videoUrl:(NSString *)videoUrl {
	self.coverView.image = [UIImage imageNamed:videoCoverUrl];
	self.videoUrl = videoUrl;
	[self.videoToolBar layoutWithModel:nil];
}

#pragma mark - private method
-(void)_tapToPlay{
	[[GTVideoPlayer Player]playWithVideoUrl:self.videoUrl attachView:self.coverView];
}

@end
